home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / CPasswordField / CPasswordField.cp next >
Encoding:
Text File  |  1995-09-04  |  3.2 KB  |  139 lines  |  [TEXT/CWIE]

  1. /*
  2.     CPasswordField.cp
  3.  
  4.     Subclass of LEditField that is used for entering passwords in such a way
  5.     that they can't be seen on screen.
  6.     
  7.     It doesn't do any sophisticated editing, if you press the delete key, the
  8.     whole field is wiped out.
  9.     
  10.     History:    
  11.         Wed, 30 Aug 1995    Peter Marks    Created.
  12.  
  13. */
  14.  
  15. #include "CPasswordField.h"
  16. #include "Utilities.h"
  17.  
  18.  
  19. CPasswordField::CPasswordField() 
  20. {
  21. }
  22.  
  23. CPasswordField::CPasswordField(LStream *inStream)
  24.     : LEditField(inStream)
  25. {
  26.     mContent[0] = 0;
  27. }
  28.  
  29. CPasswordField::~CPasswordField()
  30. {
  31. }
  32.  
  33. #pragma segment Main
  34. LEditField*    CPasswordField::CreatePasswordFieldStream(LStream *inStream)// Override
  35. {
  36.     return (new CPasswordField(inStream));
  37. }
  38.  
  39.  
  40. #pragma segment Main
  41. StringPtr    CPasswordField::GetDescriptor(Str255 outDescriptor) const// Override
  42. {
  43.     return pStrcpy(outDescriptor, (unsigned char *)mContent);
  44. }
  45.  
  46.  
  47. #pragma segment Main
  48. void        CPasswordField::SetDescriptor(
  49.                                 ConstStr255Param    inDescriptor)// Override
  50. {
  51.     ::pStrcpy(mContent, (unsigned char *)inDescriptor);
  52.     short    length = mContent[0];
  53.     Str255    visibleString;
  54.     visibleString[0] = '\0';
  55.     for(short i = 0; i < length; i++)
  56.     {
  57.         pStrcat(visibleString, "\p•");
  58.     }
  59.     LEditField::SetDescriptor(visibleString);
  60. }
  61.  
  62.  
  63. #pragma segment Main
  64. void        CPasswordField::FindCommandStatus(
  65.                                 CommandT            inCommand,
  66.                                 Boolean                &outEnabled,
  67.                                 Boolean                &outUsesMark,
  68.                                 Char16                &outMark,
  69.                                 Str255                outName)// Override
  70. {
  71.     // disable cut and copy so they can't cheat
  72.     switch(inCommand)
  73.     {
  74.         case cmd_Cut:
  75.         case cmd_Copy:
  76.             outEnabled = false;
  77.             break;
  78.         default:
  79.             LEditField::FindCommandStatus(inCommand, outEnabled, outUsesMark, outMark, outName);
  80.             break;
  81.     }
  82. }
  83.  
  84.  
  85. #pragma segment Main
  86. Boolean        CPasswordField::HandleKeyPress(
  87.                                 const EventRecord    &inKeyEvent)// Override
  88. {
  89.     // append the character to the mContent buffer
  90.     // handle delete key reasonably
  91.     // put bullets in the visible field for feedback
  92.     char theChar = inKeyEvent.message & charCodeMask;
  93.     EventRecord myKeyEvent = inKeyEvent;
  94.     // let return tab and enter pass through
  95.     if((theChar != 0x0d) && (theChar != 0x03) && (theChar != '\t'))
  96.     {
  97.         if(theChar == '\b')    // delete character
  98.         {
  99.             mContent[0] = 0;    // clear the internal buffer
  100.             SelectAll();        // and clear the visible field
  101.             ::TEDelete(mTextEditH);
  102.             return true;
  103.         }
  104.         else
  105.         {
  106.             // there are only two possible selection states, in this simple model
  107.             // none or all
  108.             // Check for any selection, otherwise clear the field when they type
  109.             if((*mTextEditH)->selStart != (*mTextEditH)->selEnd)
  110.             {
  111.                 mContent[0] = 1;    // clear the internal buffer and add the character
  112.                 mContent[1] = theChar;
  113.                 
  114.                 SelectAll();        // and clear the visible field
  115.                 ::TEDelete(mTextEditH);
  116.                 ::TEKey('•', mTextEditH);
  117.             }
  118.             else
  119.             {
  120.                 // ok, so append it to our internal buffer
  121.                 Str255    charStr;
  122.                 charStr[0] = 1;
  123.                 charStr[1] = theChar;
  124.                 ::pStrcat(mContent, charStr);
  125.                 ::TEKey('•', mTextEditH);
  126.             }
  127.             return true;
  128.         }
  129.     }
  130.     return LEditField::HandleKeyPress(myKeyEvent);
  131. }
  132.  
  133. // override to simply select all if they click in the field
  134. void CPasswordField::ClickSelf(const SMouseDownEvent & /* inMouseDown */)
  135. {
  136.     ::TESetSelect(0, 32000, mTextEditH);
  137.     SwitchTarget(this);
  138. }
  139.